//
//  ZJRegisterViewController.m
//  ZiJinLian
//
//  Created by lzy on 2017/3/9.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "ZJRegisterViewController.h"
#import "ZJSecuritycodeViewController.h"
#import "ZJNotMineViewController.h"
#import "WelcomViewController.h"
@interface ZJRegisterViewController ()<UITextFieldDelegate,UIAlertViewDelegate,ZJModelViewDelegate>
@property(nonatomic,strong)NSString * nickName;
@property(nonatomic,strong)NSString * urlStr;
@end

@implementation ZJRegisterViewController

-(void)createView{
    [self initTitle:MyString(@"注册")];
    _phoneView=[[ZJphone alloc]init];
    _phoneView.frame=self.view.frame;
    [self.view addSubview:self.phoneView];
    UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 45, 44)];
    [btn setTitle:MyString(@"注册协议") forState:0];
    [btn setTitleColor:[UIColor whiteColor] forState:0];
    btn.titleLabel.font=[UIFont systemFontOfSize:12];
    [btn addTarget:self action:@selector(editorBtnClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*item=[[UIBarButtonItem alloc] initWithCustomView:btn];
    [self.navigationItem setRightBarButtonItem:item];
  
}
-(void)editorBtnClick{
    WelcomViewController*vc=[WelcomViewController new];
    vc.reLastURL=[NSString stringWithFormat:@"%@/agreement",baseURL];
    vc.title=MyString(@"注册协议");
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)doOpeartion{
    [self.phoneView.phoneTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
     [_phoneView.nextBtn addTarget:self action:@selector(nextClick:) forControlEvents:UIControlEventTouchUpInside];
     _phoneView.phoneTF.delegate=self;
    
    [self doKeyBoardDown];

}

- (void) textFieldDidChange:(id) sender {
    UITextField *_field = (UITextField *)sender;
    NSString *str=_field.text;
    if (str) {
        [self.phoneView.nextBtn setBackgroundColor:button_Red_Color];
        [self.phoneView.nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
}


#pragma -----------键盘下降方法--------
-(BOOL) textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    return YES;
}



-(void)nextClick:(UIButton *)btn
{
    
    if (![regular checkRegular7:self.phoneView.phoneTF.text]) {
        [self showMessage:MyString(@"请填写正确的手机号")];
        return;
    }
        NSMutableDictionary *dic=[NSMutableDictionary dictionary];
        [dic setObject:self.phoneView.phoneTF.text forKey:@"phone"];
        [[ZJBaseAFManager sharedManager] GET:API_CHECK_PHONE parameters:dic progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
              
            if ([responseObject[@"status"] isEqualToNumber:@0]) {
                 [self pushViewController];
            }else {
                self.nickName=responseObject[@"data"][@"nickname"];
                self.urlStr=responseObject[@"data"][@"avatar"];
                //跳转 模态
                ZJNotMineViewController *notMineVC=[[ZJNotMineViewController alloc]init];
                notMineVC.curNickName=self.nickName;
                notMineVC.curheadurlStr=self.urlStr;
                notMineVC.delegate=self;
                [self.navigationController presentViewController:notMineVC animated:YES completion:nil];
                
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            NSLog(@"网络错误");
            
        }];
    
}

//实现 模态页面的代理方法
-(void)viewDismiss{

    ZJSecuritycodeViewController * SecuritycodeRegisterVC=[[ZJSecuritycodeViewController alloc]init];
    SecuritycodeRegisterVC.curphoneStr=self.phoneView.phoneTF.text;
    [self.navigationController pushViewController:SecuritycodeRegisterVC animated:YES];

}
-(void)viewLogin
{
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)pushViewController
{
    ZJSecuritycodeViewController * SecuritycodeRegisterVC=[[ZJSecuritycodeViewController alloc]init];
    SecuritycodeRegisterVC.inviteCode=self.phoneView.inviteTF.text;
    SecuritycodeRegisterVC.curphoneStr=self.phoneView.phoneTF.text;
    [self.navigationController pushViewController:SecuritycodeRegisterVC animated:YES];
}




@end
